Is there a way to parse data in "Object Text" into another attribute.
I want to take information provided in Object text and add it to a attribute. An example of the Data is as follows:
ID Object Text
255 {SRS-3} The thingamajig shall run at slow speed without failure.
I want
ID Object Text Old Req Number
255 The thingamajig shall run at slow speed without failure. SRS-3
I only know how to use aready writen DXL's can anyone help
Thanks
Tap5
SystemAdmin - Fri Aug 21 15:09:08 EDT 2009 |
|
Re: Parsing Object Text Peter_Albert - Mon Aug 24 04:34:06 EDT 2009
It is not fully clear from your original post, but I guess the "Old Req Number" is part of the Object Text, i.e. in the example the Object Text is "{SRS-3} The thingamajig shall run at slow speed without failure." If this is the case, then you should get familiar with regular expressions for stripping the "Old Req Num" from the Object Text.
The code fragment below is a quick hack which works for the current Object, but
-
it will remove any rich text formatting from the Object Text if there is any,
-
it assumes that the attribute "Old Req Number" exists,
-
it assumes you have edit access to the object and its attributes, and
-
it will fail if the actual Object Text contains more curly brackets than the ones around the "Old Req Number" (but this could be fixed with a modification of the regular expression)
But anyway it works as an example for regular expressions.
Regexp reg = regexp("(^\\ *{)(.+)(}\\ *)")
Object obj = current Object
string oldObjText
string oldReqNum
string newObjText
if (!null obj)
{ // Current Object is not null
oldObjText = obj."Object Text"
} // Current Object is not null
else
{ // Current Object is null --> code testing
oldObjText = "{SRS-3} The thingamajig shall run at slow speed without failure."
} // Current Object is null --> code testing
if (reg oldObjText)
{ // Apply regular expression to string
oldReqNum = oldObjText[match 2]
newObjText = oldObjText[end 0 + 1:]
if (!null obj)
{ // Current Object is not null
obj."Object Text" = newObjText
obj."Old Req Number" = oldReqNum
} // Current Object is not null
else
{ // Current Object is null --> code testing
print oldReqNum "\n" newObjText
} // Current Object is null --> code testing
} // Apply regular expression to string
Now, the regular expression is already a bit complex. This is partly due to the fact that you asked for removal of the curly brackets from the "Old Req Number": In the Object Text, it is "{SRS-3}", while in the attribute value for "Old Req Number", it is only "SRS-3".
The first part of the regular expression is (^\\ *{)
The inner part, ignoring the surrounding brackets, translates to:
^: A string starting with
\\ *: zero or more empty spaces
{: followed by an opening curly bracket
The second part is (.+), again, ignore the outer brackets and you get
.+: one or more characters except new line (This will fetch the actual "Old Req Number")
The third part is (}\\ *), again without the outer brackets it reads:
}: A closing curly bracket,
\\ * followed by zero or more empty spaces.
Now, the brackets around the three parts are put in place such that you can directly pick out individual matches.
In the code example, "oldObjText[match 0]" would be the complete matching substring, i.e. ""{SRS-3} ", but with the brackets, you can use "oldObjText[match 2]" to directly pick out the substring matching the second pair of brackets, i.e. "SRS-3".
Hope that helps,
Peter
|
|
Re: Parsing Object Text llandale - Mon Aug 24 11:12:00 EDT 2009 Peter_Albert - Mon Aug 24 04:34:06 EDT 2009
It is not fully clear from your original post, but I guess the "Old Req Number" is part of the Object Text, i.e. in the example the Object Text is "{SRS-3} The thingamajig shall run at slow speed without failure." If this is the case, then you should get familiar with regular expressions for stripping the "Old Req Num" from the Object Text.
The code fragment below is a quick hack which works for the current Object, but
-
it will remove any rich text formatting from the Object Text if there is any,
-
it assumes that the attribute "Old Req Number" exists,
-
it assumes you have edit access to the object and its attributes, and
-
it will fail if the actual Object Text contains more curly brackets than the ones around the "Old Req Number" (but this could be fixed with a modification of the regular expression)
But anyway it works as an example for regular expressions.
Regexp reg = regexp("(^\\ *{)(.+)(}\\ *)")
Object obj = current Object
string oldObjText
string oldReqNum
string newObjText
if (!null obj)
{ // Current Object is not null
oldObjText = obj."Object Text"
} // Current Object is not null
else
{ // Current Object is null --> code testing
oldObjText = "{SRS-3} The thingamajig shall run at slow speed without failure."
} // Current Object is null --> code testing
if (reg oldObjText)
{ // Apply regular expression to string
oldReqNum = oldObjText[match 2]
newObjText = oldObjText[end 0 + 1:]
if (!null obj)
{ // Current Object is not null
obj."Object Text" = newObjText
obj."Old Req Number" = oldReqNum
} // Current Object is not null
else
{ // Current Object is null --> code testing
print oldReqNum "\n" newObjText
} // Current Object is null --> code testing
} // Apply regular expression to string
Now, the regular expression is already a bit complex. This is partly due to the fact that you asked for removal of the curly brackets from the "Old Req Number": In the Object Text, it is "{SRS-3}", while in the attribute value for "Old Req Number", it is only "SRS-3".
The first part of the regular expression is (^\\ *{)
The inner part, ignoring the surrounding brackets, translates to:
^: A string starting with
\\ *: zero or more empty spaces
{: followed by an opening curly bracket
The second part is (.+), again, ignore the outer brackets and you get
.+: one or more characters except new line (This will fetch the actual "Old Req Number")
The third part is (}\\ *), again without the outer brackets it reads:
}: A closing curly bracket,
\\ * followed by zero or more empty spaces.
Now, the brackets around the three parts are put in place such that you can directly pick out individual matches.
In the code example, "oldObjText[match 0]" would be the complete matching substring, i.e. ""{SRS-3} ", but with the brackets, you can use "oldObjText[match 2]" to directly pick out the substring matching the second pair of brackets, i.e. "SRS-3".
Hope that helps,
Peter
<1> Your code fails when the Text has braces. See what I mean when you change line 12 to this:
oldObjText = "{SRS-3} The thingamajig {More} shall run at slow speed without failure."
<2> You forgot that the Text should start with an 'ID' field, '255' in the original post. The results text should include this field.
"255 {SRS-3} The thingamajig shall run at slow speed without failure."
Perhaps it would work if you changed the inside of your regExp (.+), to be any character except opening or closing brace. If you changed your regExp to not expect open-brace at the start, I suspect this code will find the LAST set of braces {More}, not the first. Again, the first field could be any characters except close-brace.
<3> As you said you lose Rich Text, specifically including any OLE diagrams.
The solution is to get the Rich Text, use findRichText() to locate the opening brace "{", use findRichText() from there to locate the next closing brace "}", extract what's in the middle and convert using plainText() to get the oldReqNum, and finally strip the braces et tal using replaceRichText().
Clever programs would first verify that the OldReqNum acually looks like one, perhaps has a dash followed by digits, and the 'ID' field is all digits. Otherwise, don't change anything.
|
|
Re: Parsing Object Text Peter_Albert - Mon Aug 24 11:42:50 EDT 2009 llandale - Mon Aug 24 11:12:00 EDT 2009
<1> Your code fails when the Text has braces. See what I mean when you change line 12 to this:
oldObjText = "{SRS-3} The thingamajig {More} shall run at slow speed without failure."
<2> You forgot that the Text should start with an 'ID' field, '255' in the original post. The results text should include this field.
"255 {SRS-3} The thingamajig shall run at slow speed without failure."
Perhaps it would work if you changed the inside of your regExp (.+), to be any character except opening or closing brace. If you changed your regExp to not expect open-brace at the start, I suspect this code will find the LAST set of braces {More}, not the first. Again, the first field could be any characters except close-brace.
<3> As you said you lose Rich Text, specifically including any OLE diagrams.
The solution is to get the Rich Text, use findRichText() to locate the opening brace "{", use findRichText() from there to locate the next closing brace "}", extract what's in the middle and convert using plainText() to get the oldReqNum, and finally strip the braces et tal using replaceRichText().
Clever programs would first verify that the OldReqNum acually looks like one, perhaps has a dash followed by digits, and the 'ID' field is all digits. Otherwise, don't change anything.
Hi Louie,
thanks for your comments.
<1> I wrote exactly this in the introductory bullets. The regexp actually gets everything from the very first to the very last bracket (at least in v7.1, the behaviour changed in v.9, I think). As I said, one can change the regexp to actually fetch the string in the first set of brackets, "A-Z-0-9" would be a good start, but without more information from the OP, the code was not meant to completely solve the problem once and for all, but rather be a starting point for the OP to build a solution.
<2> I guess not. I read the original post as if "255" is actually the value of yet another attribute, "ID" (or the Absolute Number, perhaps). I am interpreting the original post as a table there. This is why my proposed solution starts with the bracket. But of course the regexp could be adjusted if this is not the case. BTW, if you remove the "^", the regexp will find the first set of braces.
<3> I fully agree, the OP has to judge whether his problem involves rich text or not.
On your last note: I am interpreting the original post such that OldReqNum is empty at the start in any case. And I guess "ID" is actually the Absolute Number. But let's wait for the OP ...
Regards,
Peter
|
|
Re: Parsing Object Text Peter_Albert - Mon Aug 24 11:45:47 EDT 2009 Peter_Albert - Mon Aug 24 11:42:50 EDT 2009
Hi Louie,
thanks for your comments.
<1> I wrote exactly this in the introductory bullets. The regexp actually gets everything from the very first to the very last bracket (at least in v7.1, the behaviour changed in v.9, I think). As I said, one can change the regexp to actually fetch the string in the first set of brackets, "A-Z-0-9" would be a good start, but without more information from the OP, the code was not meant to completely solve the problem once and for all, but rather be a starting point for the OP to build a solution.
<2> I guess not. I read the original post as if "255" is actually the value of yet another attribute, "ID" (or the Absolute Number, perhaps). I am interpreting the original post as a table there. This is why my proposed solution starts with the bracket. But of course the regexp could be adjusted if this is not the case. BTW, if you remove the "^", the regexp will find the first set of braces.
<3> I fully agree, the OP has to judge whether his problem involves rich text or not.
On your last note: I am interpreting the original post such that OldReqNum is empty at the start in any case. And I guess "ID" is actually the Absolute Number. But let's wait for the OP ...
Regards,
Peter
correction: the regexp fragment in <1> should of course read "[A-Z]+-[0-9]+"
|
|
Re: Parsing Object Text SystemAdmin - Mon Aug 24 12:02:53 EDT 2009
Peter and everyone
Thanks so much for the quick help. Peter you are correct in your assumtions I was trying to show the example as a table quickly and the spaces between each data elements were eliminated when I posted. I will have to get better with the posting obtions. That aside I was able to take the script and apply to the module. It works great and as advertised. Thanks again for all your help.
Thanks
Tap 5
|
|